﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AnimalWithEvents
{
    public class Animal
    {
        private string name;
        private string kind;
        protected event PropertyChangedEventHandler propertyChanged;

        public string Name
        {
            get { return this.name; }
            set
            {
                string oldValue = this.Name;
                this.name = value;
                if (oldValue != value)
                {
                    OnPropertyChanged("Animal.Name", oldValue, value);
                }
            }
        }

        public string Kind
        {
            get { return this.kind; }
            set
            {
                string oldValue = this.Kind;
                this.kind = value;
                if(oldValue != value)
                {
                    OnPropertyChanged("Animal.Kind", oldValue, value);
                }
            }
        }

        public Animal()
        {
        }

        public Animal(string name, string kind)
        {
            this.Name = name;
            this.Kind = kind;
        }

        protected void OnPropertyChanged(string propertyName, string oldValue, string newValue)
        {
            if(this.propertyChanged != null)
            {
                PropertyChangedEventArgs eventArgs = new PropertyChangedEventArgs(propertyName, oldValue, newValue);
                this.propertyChanged(this, eventArgs);
            }
        }

        public event PropertyChangedEventHandler PropertyChanged
        {
            add
            {
                this.propertyChanged += value;
                Console.WriteLine("Subscribed to Animal.PropertyChanged event.");
            }
            remove
            {
                this.propertyChanged -= value;
                Console.WriteLine("Unsubscribed to Animal.PropertyChanged event.");
            }
        }
    }
}
